package com.qqtech.qquan.push.controller;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.qqtech.admin.context.AdminPage;
import com.qqtech.core.frame.model.PageKit;
import com.qqtech.core.frame.model.PageOrder;
import com.qqtech.core.frame.model.Result;
import com.qqtech.core.frame.service.BaseService;
import com.qqtech.qquan.community.model.CommunityBase;
import com.qqtech.qquan.community.service.CommunityBaseService;
import com.qqtech.qquan.mall.model.MallShopVo;
import com.qqtech.qquan.mall.service.MallShopService;
import com.qqtech.qquan.push.enums.PushBizzEnum;
import com.qqtech.qquan.push.model.PushLog;
import com.qqtech.qquan.push.model.PushLogQuery;
import com.qqtech.qquan.push.model.PushLogVo;
import com.qqtech.qquan.push.service.PushAudienceService;
import com.qqtech.qquan.push.service.PushBizzService;
import com.qqtech.qquan.sys.controller.BaseAdminController;
import com.qqtech.qquan.user.model.UserBase;
import com.qqtech.qquan.user.service.UserBaseService;

@Controller
@RequestMapping(value = "/push")
public class PushBaseController extends BaseAdminController {
	@Resource
	private PushBizzService pushBizzService;
	@Resource
	private MallShopService mallShopService;
	@Resource
	private PushAudienceService pushAudienceService;

	@Resource
	private UserBaseService userBaseService;

	@Resource
	private CommunityBaseService communityBaseService;

	@Override
	protected BaseService<PushLog> getBaseService() {
		return pushBizzService;
	}

	/**
	 * 初始化用户的推送标签和别名(用户的社区id为标签，用户id为别名)
	 * 
	 * @param userId
	 * @return
	 */
	@RequestMapping(value = "/initUser")
	@ResponseBody
	public Result initUser(String userId) {
		int i = 0;
		Result result = new Result();
		List<UserBase> all = userBaseService.queryAll();
		Integer communityId = null;
		for (UserBase userBase : all) {
			communityId = userBase.getCommunityId();
			if (communityId != null && communityId.intValue() != 0) {
				pushAudienceService.updateTagAlias(userBase.getId(), communityId, null);
				i++;
			}
		}
		result.put("message", "一共完成" + i + "个用户的推送数据初始化");
		return result;
	}

	@RequestMapping(value = "/list", method = RequestMethod.GET)
	public String list() {
		return "/push/push-list";
	}

	@RequestMapping(value = "/list", method = RequestMethod.POST)
	@ResponseBody
	public AdminPage list(PushLogQuery query, HttpServletRequest request, HttpServletResponse response) {
		AdminPage adminPage = new AdminPage();
		String rows = (String) request.getParameter("rows"); // 每页条数
		query.setSize(Integer.valueOf(rows));

		Pageable pageKit = new PageKit(query.getPage(), query.getSize(), new PageOrder(Direction.DESC, "newAt"));
		Page<PushLogVo> page = pushBizzService.queryPageList(query, pageKit);
		for (PushLogVo pushLog : page) {
			if (pushLog.getCommunityId() != null) {
				CommunityBase communityBase = communityBaseService.getById(pushLog.getCommunityId());
				if (communityBase != null) {
					pushLog.setCommunityName(communityBase.getName());
				}
			}
			pushLog.setPushBizzName(PushBizzEnum.getDesc(pushLog.getPushBizz()));
		}
		adminPage.setRows(page.getContent());
		adminPage.setTotal(Integer.valueOf(page.getTotalElements() + ""));
		return adminPage;
	}

	/**
	 * 执行推送
	 * 
	 * @param PushLog
	 * @return
	 */
	@RequestMapping(value = "/doPush")
	@ResponseBody
	public Result doPush(String title, String content, String url, Integer shopId, Integer communityId) {
		Result ret = new Result();
		if (shopId != null) {
			pushBizzService.shopNotice(title, content, url, shopId, communityId);
			return ret;
		}
		pushBizzService.adminNotice(title, content, url, communityId);
		return ret;
	}

	/**
	 * 打开推送页面
	 * 
	 * @param
	 * @return
	 */
	@RequestMapping(value = "/toPush")
	public String toPush(Model model) {
		List<MallShopVo> mallShopVos = mallShopService.queryALl();
		model.addAttribute("mallShopVos", mallShopVos);
		return "/push/push-add";
	}
}
